class Solution {
public:
    int wiggleMaxLength(vector<int>& a) {
        int n = a.size();
        vector<int>f(n, 1), g(n, 1);
        int ma = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (a[j] < a[i]) {
                    f[i] = max(g[j] + 1, f[i]);
                }
                else if (a[j] > a[i]) {
                    g[i] = max(f[j] + 1, g[i]);
                }
            }
            ma = max(ma, max(g[i], f[i]));
        }
        return ma;
    }
};